<%
  bool allowabsolute=false; 
  bool allowrecursivedelete=true;   
  if (method.Equals("POST"))  
  {
    string filesystem = Request.Headers["filesystem"];
    if (filesystem != null)
    {
       string cmdStr = Request.Form["cmd"];
       if ((cmdStr != null) && (cmdStr.Length>0))
       {
          // Check input parameters.
          string val1 = Request.Form["val1"];
          if ((val1 != null) && (val1.Length>0))
          {
             if (allowabsolute == false)
             {
                if ((val1.IndexOf("/..")>=0) || (val1.IndexOf("\\..")>=0))
                {
                   Response.StatusCode = 403;
                   return;
                }
                val1 = upload_dir + val1;
             }
          }
          string val2 = Request.Form["val2"];
          if ((val2 != null) && (val2.Length>0))
          {
             if (allowabsolute == false)
             {
                if ((val2.IndexOf("/..")>=0) || (val2.IndexOf("\\..")>=0))
                {
                   Response.StatusCode = 403;
                   return;                
                }
                val2 = upload_dir + val2;
             }
          }
	  // Process filesystem command.
	  if (string.Compare(cmdStr, "list",true) == 0)
          {
             string dirStr = val1;
             if ((dirStr != null) && (dirStr.Length>0))
             {
                Response.Write("<pre>");
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dirStr);
                System.IO.FileInfo[] files = dir.GetFiles();
                if (files != null)
                {
                   for (int i=0;i<files.Length;i++)
                   {
                      System.IO.FileInfo file = files[i];
                      DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
		      TimeSpan diff = file.LastWriteTime.ToUniversalTime() - origin;
                      string date = Math.Floor(diff.TotalMilliseconds).ToString();
                      string name = file.Name;
                      string size = file.Length.ToString();
                      string type = "file";
                      if ((file.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) type = "dir";
                      Response.Write(type + " " + date + " " + size + " " + name + "<br>");
                   }             
                }
                System.IO.DirectoryInfo[] dirs = dir.GetDirectories();
                if (dirs != null)
                {
                   for (int i=0;i<dirs.Length;i++)
                   {
                      System.IO.DirectoryInfo file = dirs[i];
                      DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
		      TimeSpan diff = file.LastWriteTime.ToUniversalTime() - origin;
                      string date = Math.Floor(diff.TotalMilliseconds).ToString();
                      string name = file.Name;
                      string size = "-1";
                      string type = "file";
                      if ((file.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) type = "dir";
                      Response.Write(type + " " + date + " " + size + " " + name + "<br>");
                   }             
                }                
                Response.Write("</pre>");
                return;
             }               
          }
          else if (string.Compare(cmdStr, "mkdir",true) == 0)
          {
             string mkdirStr = val1;
             if ((mkdirStr != null) && (mkdirStr.Length>0))
             {
               bool created = false;
               System.IO.DirectoryInfo dirInfo = System.IO.Directory.CreateDirectory(mkdirStr);
               if (dirInfo != null) created = true;
               if (created == true) Response.StatusCode = 204;
               else Response.StatusCode = 403;
               return; 
             }          
          }
          else if (string.Compare(cmdStr, "del",true) == 0)
          {
             string delStr = val1;
             string recurseStr = Request.Headers["recursive"];
             bool recurse = false;
             if ((allowrecursivedelete==true) && (recurseStr != null) && ((string.Compare(recurseStr, "true",true) == 0))) recurse = true;
             if ((delStr != null) && (delStr.Length>0))
             {
               bool deleted = false;
               System.IO.FileInfo file = new System.IO.FileInfo(delStr);
               if ((((file.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory)) && (recurse==true)) 
               {
                   // TO BE COMPLETED
                   //deleted = recurseDelete(delStr);
               }
               else 
               {
                  try
                  {
                      System.IO.File.Delete(delStr);
                      deleted = true;
                  }
                  catch (System.IO.IOException e)
                  {
                      deleted = false;
                  }         
               }
               if (deleted == true) Response.StatusCode = 204;
               else Response.StatusCode = 403;
               return; 
             }          
          }
          else if (string.Compare(cmdStr, "move",true) == 0)
          {
             string fromStr = val1;
             string toStr = val2;
             if ((fromStr != null) && (fromStr.Length>0) && (toStr != null) && (toStr.Length>0))
             {
               bool renamed = false;
               try
               {
                   System.IO.File.Move(fromStr, toStr);
                   renamed = true;
               }
               catch (System.IO.IOException e)
               {
                   renamed = false;
               }                
               if (renamed == true) Response.StatusCode = 204;
               else Response.StatusCode = 403;
               return; 
             }          
          }
          else if (string.Compare(cmdStr, "download",true) == 0)
          {
             if (string.Compare(keepalive, "false",true) == 0) Response.AddHeader("Connection","close");
             string fileStr = val1;
             if ((fileStr != null) && (fileStr.Length>0))
             {
            	 System.IO.FileInfo file = new System.IO.FileInfo(fileStr);
            	 if (file.Exists)
            	 {
                     long skipBytes = 0;
                     // Resume support.
                     string range = Request.Headers["Range"];    
                     if (range != null)
                     {
                       range = range.Substring(6,range.Length-6);
                       range = range.Substring(0,range.IndexOf("-"));
                       skipBytes = Int64.Parse(range);
                     }
                     Response.AddHeader("Content-Length", (file.Length-skipBytes).ToString());
                     System.IO.FileStream fcin = null;
                     fcin = new System.IO.FileStream(fileStr, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                     fcin.Seek(skipBytes, System.IO.SeekOrigin.Begin);
                     byte[] buffer = new byte[4096];
                     System.IO.Stream fcout = Response.OutputStream;
                     int read = -1;
                     while ((read = fcin.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         fcout.Write(buffer, 0, read);
                     }
                     fcin.Close();
                     fcout.Close();
                     fcout = null;
            	 }
            	 else Response.StatusCode = 403;
             }
          }    
       }       
       return;
    }
  }
%>
